import request from "@/utils/request";

const AUTH_BASE_URL = "/core/login";
// const AUTH_BASE_URL = "/api/v1/auth";

const AuthAPI = {
  /** 登录接口*/
  // login(data: LoginData) {
  //   const formData = new FormData();
  //   formData.append("username", data.username);
  //   formData.append("password", data.password);
  //   formData.append("captchaKey", data.captchaKey);
  //   formData.append("captchaCode", data.captchaCode);
  //   return request<any, LoginResult>({
  //     url: `${AUTH_BASE_URL}/login`,
  //     method: "post",
  //     data: formData,
  //     headers: {
  //       "Content-Type": "multipart/form-data",
  //     },
  //   });
  // },

  login(data: LoginData) {
    const formData = new FormData();
    formData.append("account", data.account);
    formData.append("password", data.password);
    formData.append("code", data.code);
    formData.append("inputCode", data.inputCode);
    return request<any, LoginResult>({
      url: `${AUTH_BASE_URL}/loginSysUser`,
      method: "post",
      data: formData,
      headers: {
        "Content-Type": "application/json",
      },
    });
  },

  /** 刷新 token 接口*/
  refreshToken(refreshToken: string) {
    return request<any, LoginResult>({
      url: `${AUTH_BASE_URL}/refresh-token`,
      method: "post",
      data: { refreshToken: refreshToken },
      headers: {
        Authorization: "no-auth",
      },
    });
  },

  /** 注销接口*/
  logout() {
    return request({
      // url: `${AUTH_BASE_URL}/logout`,
      url: `/core/login/logoutUser`,
      method: "GET",
    });
  },

  /** 获取验证码接口*/
  getCaptcha() {
    return request<any, CaptchaResult>({
      // url: `${AUTH_BASE_URL}/captcha`,
      url: `${AUTH_BASE_URL}/getValidateCode`,
      method: "get",
    });
  },
};

export default AuthAPI;

/** 登录请求参数 */
// export interface LoginData {
//   /** 用户名 */
//   username: string;
//   /** 密码 */
//   password: string;
//   /** 验证码缓存key */
//   captchaKey: string;
//   /** 验证码 */
//   captchaCode: string;
// }

export interface LoginData {
  /** 用户名 */
  account: string;
  /** 密码 */
  password: string;
  /** 验证码缓存key */
  code: string;
  /** 验证码 */
  inputCode: string;
}

/** 登录响应 */
export interface LoginResult {
  /** 访问令牌 */
  accessToken: string;
  /** 刷新令牌 */
  refreshToken: string;
  /** 令牌类型 */
  tokenType: string;
  /** 过期时间(秒) */
  expiresIn: number;

  //Token
  saTokenInfo: {
    tokenValue: string;
  };
}

/** 验证码响应 */
export interface CaptchaResult {
  /** 验证码缓存key */
  // captchaKey: string;
  code: string;
  /** 验证码图片Base64字符串 */
  // captchaBase64: string;
  base64: string;
}
